﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace typewriter
{
    public partial class Frmtype : Form
    {

        public bool needSaving = false;
        public string path = "";

        public Frmtype()
        {
            InitializeComponent();
            
        }
        public void takeit(int x)
        {

        }

        private void Frmtype_Load(object sender, EventArgs e)
        {
           // 
            tw.trigger("load", this);
            sizeup();
        }

        private void sizeup()
        {
            /*

            Size x = SystemInformation.PrimaryMonitorSize;

            this.Width = x.Width;
            this.Height = x.Height;
            this.Location = new Point(0, 0);

            this.rtb.Width = 800;
            this.rtb.Left = (x.Width - 800) / 2;

            this.rtb.Height = 1000;
            this.rtb.Top = (x.Height - 1000) / 2;
            */
            tw.trigger("size", this);
            
        }


        private void button1_Click(object sender, EventArgs e)
        {
            Point p = this.Location;
           
            mnuMain.Show(p);
        }

        private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmSettings f = new FrmSettings();
            f.Show();
        }

        private void killToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (needSaving)
            {

            }
            else
            {
                this.Close();
            }
        }

        public void open_file()
        {

            OpenFileDialog dlg = new OpenFileDialog();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                do_open(dlg.FileName);

            }
        }

        public void save_as()
        {
            saveFileDialog1.Title = "Specify Destination Filename";
            saveFileDialog1.Filter = "Text Files|*.txt";
            saveFileDialog1.FilterIndex = 1;
            saveFileDialog1.OverwritePrompt = true;

            if (saveFileDialog1.ShowDialog() != DialogResult.Cancel)
            {
                path = saveFileDialog1.FileName;
                tsPath.Text = "PATH: " + path;
                do_save();
            }

        }

        public void do_save()
        {
            TextWriter tw = new StreamWriter(path);
            tw.WriteLine(rtb.Text);
            tw.Close();

            needSaving = false;
        }

        public void do_open(string opath)
        {
            path = opath;
            TextReader tr = new StreamReader(opath);
            rtb.Text = tr.ReadToEnd();
            tr.Close();
            tsPath.Text = "PATH: " + path;
            needSaving = false;
        }

        private void rtb_TextChanged(object sender, EventArgs e)
        {
            if(!needSaving)
                needSaving = true;


        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (path.Length < 1)
            {
                save_as();
            }
            else
            {
                do_save();
            }
        }

        private void loadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            open_file();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

        }

        private void Frmtype_Resize(object sender, EventArgs e) {
            tw.trigger("size", this);
        }

        private void Frmtype_Activated(object sender, EventArgs e) {
            tw.setActive(this);
        }








    }
}
